﻿Data = {};

var DataInput = {
	initialize:function()
	{
		this.chart_update = $("#chart_update");
		this.profile_img = $("#profile_img");
		this.nickname = $("#nickname");
		this.sex = $("#sex");
		this.age = $("#age");
		this.rm_score_side = $("#rm_score_side");
		this.member_score = $("#member_score");
		this.self_intro_content = $("#self_intro_content");
		this.rm_score = $("#rm_score");
		this.target_weight = $("#target_weight");
		this.first_symptom = $("#first_symptom");
		this.recent_check = $("#recent_check");
		this.present_hospital = $("#present_hospital");
		this.present_treatment = $("#present_treatment");
		this.present_symptom = $("#present_symptom");
		
		this.nowTime = new Date();
	},
	defaultDataInput:function()
	{
		Data.chart_update = "2011-11-09";
		Data.src_profileimg = "images/profile/profile_img.jpg";
		Data.nickname = "hhh";
		Data.sex = "남";
		Data.age = "20";
		Data.ra_score = "4";
		Data.member_score = "2";
		
		Data.member_info = "이런 곳이 있는 줄 몰랐군요. 처음 뵙겠습니다. 서로 힘이 되는 이야기 많이 나눠요. :)";
		Data.weight = "75";
		Data.target_weight = "66";
		Data.first_report_date = "2009-09-10";
		Data.last_report_date = "2011-11-11";
		
		Data.hospital_list = ['서울대학변원', 'NextDoorDoctor'];
		Data.cure_list = [['유한레트트렉세이트정 2.5 mg','일주일 1회 (67일 남음)'],
		                  ['플린정 1 mg','일주일 1회 (43일 남음)'],
		                  ['아라바정 20 mg','하루 3회 (27일 남음)'],
		                  ['아스피린 300 mg','하루 1회 (31일 남음)']];
		Data.symptom_list = ['조조장직 · 조기피로 · 손저림','불안 · 우울'];
	},
	onRenderingData:function()
	{
		//사이드 자기 정보
		$(this.profile_img).attr('src', Data.src_profileimg);
		$(this.sex).html(Data.sex+" ·");
		$(this.age).html(Data.age+"세");
		$(this.nickname).html(Data.nickname);
		
		//자기 소개
		$(this.self_intro_content).html(Data.member_info);
		
		//건강 정보 + 사이드 류마티스 및 회원 정보
		var rm_score_side_c = "";
		var rm_score_c = "";
		var member_score_c = "";
		
		for (var i=0;i<Data.ra_score;i++){
			rm_score_side_c += "☻"; 
			rm_score_c += "☻";
		}
		while (rm_score_c.length < 5){ rm_score_c +="☺"; }
		
		for (var i=0;i<Data.member_score;i++){
			member_score_c += "★";
		}
		
		$(this.rm_score_side).html(Data.ra_score + "점" + " " + rm_score_side_c);
		$(this.member_score).html(Data.member_score + "급" + " " + member_score_c);
		
		$(this.self_intro_content).html(Data.member_info);
		
		$(this.rm_score).html(rm_score_c);
		
		//Calculate the Remain Weight
		var remain_weight = Data.target_weight - Data.weight;
		if (remain_weight > 0){
			remain_weight = remain_weight + " kg 증가시키세요.";
		}else if (remain_weight < 0){
			remain_weight = String(remain_weight).substr(1,3) + " kg 감량시키세요.";
		}else{
			remain_weight = "현재 상태를 유지하세요!"
		}
		
		$(this.target_weight).html(remain_weight);
		
		//Calculate the First and Last REPORT DATE
		var d = new Date();
		var s = this.leadingZeros(d.getFullYear(), 4) + '-' +
				this.leadingZeros(d.getMonth() + 1, 2) + '-' +
				this.leadingZeros(d.getDate(), 2);

		// 음수값이 안 나오도록, from 과 to 의 자리를 바꿈 
		var chart = this.calDateRange(Data.chart_update, s)
		var first = this.calDateRange(Data.first_report_date, s);
		var recent = this.calDateRange(Data.last_report_date, s);
		
		$(this.chart_update).html(chart);
		$(this.first_symptom).html(first+"일 전");
		$(this.recent_check).html(recent+"일 전");
		
		//증상 및 치료 정보
		var present_hospital = "";
		var present_treatment = "";
		var present_symptom = "";
		
		for(var i=0;i<(Data.hospital_list).length;i++){
			present_hospital += Data.hospital_list[i];
			if (i != (Data.hospital_list).length){
				present_hospital += "<br>";
			}
		}
		for(var i=0;i<(Data.cure_list).length;i++){
			for(var j=0;j<(Data.cure_list[i]).length;j++){
				if (j == 0){
					present_treatment += Data.cure_list[i][j];
				}else{
					var cure_st = "<span class='cure_date'>" + Data.cure_list[i][j] + "</span>";
					present_treatment += cure_st;
				}
				if (!(i == (Data.cure_list).length && j != 0)){
					present_treatment += "<br>";
				}
			}
		}
		for(var i=0;i<(Data.symptom_list).length;i++){
			present_symptom += Data.symptom_list[i];
			if (i != (Data.symptom_list).length){
				present_symptom += "<br>";
			}
		}
		$(this.present_hospital).html(present_hospital);
		$(this.present_treatment).html(present_treatment);
		$(this.present_symptom).html(present_symptom);
	},
	calDateRange:function(val1, val2)
    {
        var FORMAT = "-";
        
        // FORMAT을 포함한 길이 체크
        if (val1.length != 10 || val2.length != 10)
            return null;

        // FORMAT이 있는지 체크
        if (val1.indexOf(FORMAT) < 0 || val2.indexOf(FORMAT) < 0)
            return null;

        // 년도, 월, 일로 분리
        var start_dt = val1.split(FORMAT);
        var end_dt = val2.split(FORMAT);

        // 월 - 1(자바스크립트는 월이 0부터 시작하기 때문에...)
        // Number()를 이용하여 08, 09월을 10진수로 인식하게 함.
        start_dt[1] = (Number(start_dt[1]) - 1) + "";
        end_dt[1] = (Number(end_dt[1]) - 1) + "";

        var from_dt = new Date(start_dt[0], start_dt[1], start_dt[2]);
        var to_dt = new Date(end_dt[0], end_dt[1], end_dt[2]);

        return (to_dt.getTime() - from_dt.getTime()) / 1000 / 60 / 60 / 24;
    },
    leadingZeros:function(n, digits)
    {
    	  var zero = '';
    	  n = n.toString();

    	  if (n.length < digits) {
    	    for (i = 0; i < digits - n.length; i++)
    	      zero += '0';
    	  }
    	  return zero + n;
    }
}